﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using System.Configuration;

namespace Tools
{
    public class SingleKey
    {
        public static bool CheckKey(string Key)
        {
            if (FormsAuthentication.HashPasswordForStoringInConfigFile(DateTime.Now.ToString("yyyy_MM_dd") + ConfigurationManager.AppSettings["SingleKey"], "MD5") == Key)
                return true;
            else if (FormsAuthentication.HashPasswordForStoringInConfigFile(DateTime.Now.AddDays(-1).ToString("yyyy_MM_dd") + ConfigurationManager.AppSettings["SingleKey"], "MD5") == Key)
                return true;
            else if (FormsAuthentication.HashPasswordForStoringInConfigFile(DateTime.Now.AddDays(+1).ToString("yyyy_MM_dd") + ConfigurationManager.AppSettings["SingleKey"], "MD5") == Key)
                return true;

            return false;
        }       
        public static string GetKey()
        {
            return FormsAuthentication.HashPasswordForStoringInConfigFile(DateTime.Now.ToString("yyyy_MM_dd") + ConfigurationManager.AppSettings["SingleKey"], "MD5");
        }        
        public static void CheckKeyThrow(string Key)
        {
            if (!CheckKey(Key))
                throw new Exception("无法通过系统验证，请遵从正常的流程进行操作或者重新登陆再试。");
        }
    }
}
